# -*- coding: utf-8 -*-
# @File     : throttling.py
# @Author   : bingjia
# @Time     : 2020/8/4 14:01
# @Desc     : auth 节流

from ..base.api import throttling


class LoginFailRateThrottle(throttling.UserRateThrottle):
    scope = "login-fail"
    throttled_actions = ["login"]

    def throttle_success(self, request, view):
        return True

    def finalize(self, request, response, view):
        if response.status_code == 400:
            self.history.insert(0, self.now)
            self.cache.set(self.key, self.history, self.duration)


class RegisterSuccessRateThrottle(throttling.UserRateThrottle):
    scope = "register-success"
    throttled_actions = ["register"]

    def throttle_success(self, request, view):
        return True

    def finalize(self, request, response, view):
        if response.status_code == 201:
            self.history.insert(0, self.now)
            self.cache.set(self.key, self.history, self.duration)


